package com.cumulus.MoveCopy;

import java.util.ArrayList;

import com.cumulus.MoveCopy.R.string;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ShareCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;

/*
 * Class TransitionActivity
 * Both 
 */

public class ScoreActivity extends Activity {

	private static final String TAG = "TransitionActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.score_board);
		Log.d(TAG,"created");
		ArrayList<Integer> rounds_played = this.getIntent()
				.getExtras().getIntegerArrayList("rounds_played");
		boolean toend = this.getIntent().getExtras()
				.getBoolean("endgame");
		int round = this.getIntent().getExtras()
				.getInt("num_rounds");
		Log.d(TAG,"created"+toend);
		((TextView)findViewById(R.id.score_title)).setText("Round "+(round));

		if(!toend){
			((TextView)findViewById(R.id.mainMenu)).setText(string.score_button);
			((TextView)findViewById(R.id.score_title)).setHeight(60);
			((Button)findViewById(R.id.mainMenu)).setOnClickListener(newRoundListener);
		}

		Log.d(TAG,"score activity");
		ArrayList<Integer> players = this.getIntent().getExtras()
				.getIntegerArrayList("player_arr");
		Log.d(TAG,players.toString());
		ArrayList<Integer> values = new ArrayList<Integer>();
		ArrayList<String> strings = new ArrayList<String>();
		boolean add=true;

		values.add(players.get(0)/rounds_played.get(0));
		strings.add(".        Player "+1+"        "+players.get(0)+"%");

		if (players.size()>0){
		for(int i = 1; i < players.size(); i++){
			for(int z = 0; z < strings.size(); z++){
				if(values.get(z)<players.get(i)/rounds_played.get(i)){
					Log.d(TAG,strings.toString());
					strings.add(z,".        Player "+(i+1)+"        "+players.get(i)+"%");
					values.add(z,players.get(i)/rounds_played.get(i));
					add=false;
					z=strings.size()+1;
				}
			}
			if (add){
				strings.add(".        Player "+(i+1)+"        "+players.get(i)/rounds_played.get(i)+"%");
				values.add(players.get(i)/rounds_played.get(i));
				
			}
			add=true;
		}
		}
		
		Log.d(TAG,strings.toString());
		TableLayout tl = (TableLayout) findViewById(R.id.test_table);

		/* Create a new row to be added. */
	
		for (int i = 0; i < strings.size(); i++) {
			TableRow tr = new TableRow(this);
			tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			/* Create a Button to be the row-content. */
			TextView b = new TextView(this);
			b.setTextSize(40);
			b.setText(i+1+strings.get(i));
			b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
			/* Add Button to row. */
			tr.addView(b);
			tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT));
		}
		
		/* Add row to TableLayout. */


	}
	
	public void shareResults(View view) {

        ShareCompat.IntentBuilder shareIntentBuilder = ShareCompat.IntentBuilder.from(this)
                .setText("I was " + "% accurate in copying my friend using MoveCopy. Try out this app. It's so cool!")
                .setType("text/plain");
        startActivity(shareIntentBuilder.getIntent());
        
	}


	public void goToMainActivity(View view) {
		Log.d(TAG, "transition activity called");

		// Log.d(TAG, "number of players: "+ number_players);
		Intent intent = new Intent(ScoreActivity.this, MainActivity.class);
		startActivity(intent);
	}
	/*
	 * startReplyActivity Get number_players, player ArrayList, current_index,
	 * recorder_index And pass, without change, to ReplyActivity.
	 */

    View.OnClickListener newRoundListener = new View.OnClickListener() {
        public void onClick(View view) {   
		Log.d(TAG, "transition activity called");

		// Log.d(TAG, "number of players: "+ number_players);
		Intent intent = new Intent(ScoreActivity.this, CreateOwnActivity.class);
		intent.putExtras(ScoreActivity.this.getIntent().getExtras());

		startActivity(intent);
	}
    };
}
